package com.heima.user.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.heima.common.constants.BehaviorConstants;
import com.heima.common.redis.CacheService;
import com.heima.model.behavior.dtos.FollowBehaviorDto;
import com.heima.model.behavior.pojo.ApUserFan;
import com.heima.model.behavior.pojo.ApUserFollow;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.pojos.ApUser;
import com.heima.user.mapper.ApUserFanMapper;
import com.heima.user.mapper.ApUserFollowMapper;
import com.heima.user.mapper.ApUserMapper;
import com.heima.user.service.UserFollowService;
import com.heima.utils.common.AppThreadLocalUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

/**
 * @author 熏橙橙
 * @version 1.0
 * @date 2023-08-02 17:18
 */
@Service
@Slf4j
@Transactional
public class UserFollowServiceImpl implements UserFollowService {

    @Autowired
    private CacheService cacheService;
    @Autowired
    private ApUserFanMapper apUserFanMapper;
    @Autowired
    private ApUserFollowMapper apUserFollowMapper;
    @Autowired
    private ApUserMapper apUserMapper;
    /**
     * 处理用户关注
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult user_follow(FollowBehaviorDto dto) {
        ApUser user = AppThreadLocalUtil.getUser();
        if (user==null||user.getId()==null
                ||dto==null
                ||dto.getOperation()==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        /*
        一共要处理三张表
        redis里添加上一个key=follow+userId+articleId的值
        userfan中添加一条粉丝数据
        userfollow表中添加一条关注数据
         */
        String key = BehaviorConstants.FOLLOW+user.getId()+"_"+dto.getAuthorId();

        /*ApUser userFollow = apUserMapper.selectById(user.getId());
        ApUser apUser = apUserMapper.selectById(dto.getArticleId());*/

        if (dto.getOperation()==0){
           /* ApUserFan apUserFan = ApUserFan.builder()
                    .userId(dto.getFollowId())
                    .fansId(user.getId())
                    .fansName(userFollow.getName())
                    .level((short) 0)
                    .isDisplay((short) 1)
                    .created_time(LocalDateTime.now())
                    .build();*/

            cacheService.set(key, JSON.toJSONString(dto));//插入redis一条数据
            //apUserFanMapper.insert(apUserFan);//插入粉丝表用户数据

            /*ApUserFollow apUserFollow = ApUserFollow.builder()
                    .userId(user.getId())
                    .followId(dto.getFollowId())
                    .followName(apUser.getName())
                    .created_time(LocalDateTime.now())
                    .level((short)1)
                    .build();*/

            //apUserFollowMapper.insert(apUserFollow);//插入关注表作者信息
        }else {
            cacheService.delete(key);//从redis删除信息
            //apUserFanMapper.delete(Wrappers.<ApUserFan>lambdaQuery().eq(ApUserFan::getFansId,user.getId()));//从粉丝表删除用户自己
            //apUserFollowMapper.delete(Wrappers.<ApUserFollow>lambdaQuery().eq(ApUserFollow::getFollowId,dto.getFollowId()));//从关注列表删除作者信息
        }


        return ResponseResult.okResult(200,dto.getOperation()==0?"关注成功":"取消关注成功");
    }
}
